package com.kotlin.app.ui

import android.view.Gravity
import android.view.View
import android.view.ViewManager
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.kotlin.app.R
import org.jetbrains.anko.*

class TitleBarUI(var titleBackgroudResource : Int = R.color.colorPrimary,
                 var backSize : Int = 20,
                 var backImageResource : Int = 0,
                 var backImageVisibility : Int = View.VISIBLE,
                 var titleText : String,
                 var rightSize : Int = 20,
                 var rightText : String,
                 var rightTextColorRescource : Int = R.color.back,
                 var rightTextVisibility : Int = View.GONE) {

    lateinit var titleRelativeLayout: RelativeLayout
    lateinit var backRelativeLayout: RelativeLayout
    lateinit var rightRelativeLayout: RelativeLayout
    lateinit var backImage: ImageView
    lateinit var titleView: TextView
    lateinit var rightView: TextView

    fun getTitleBar(viewManager: ViewManager) : RelativeLayout{
        titleRelativeLayout = with(viewManager){
            relativeLayout {
                padding = dip(15)
                backgroundResource = titleBackgroudResource

                if (backImageResource != 0){
                    backRelativeLayout = relativeLayout {
                        leftPadding = dip(10)
                        rightPadding = dip(10)
                        backImage = imageView(backImageResource){
                            visibility = backImageVisibility
                            leftPadding = dip(10)
                            rightPadding = dip(10)
                        }.lparams(width = dip(backSize), height =  dip(backSize)){
                            gravity = Gravity.CENTER
                        }
                    }.lparams(width = wrapContent, height = matchParent){
                        gravity = Gravity.LEFT and Gravity.CENTER_VERTICAL
                    }

                }

                titleView = textView {
                    text = titleText
                    textSize = dip(18).toFloat()
                    textColorResource = R.color.back
                }.lparams(width = wrapContent, height = wrapContent){
                    gravity = Gravity.CENTER
                }

                rightRelativeLayout = relativeLayout {
                    leftPadding = dip(10)
                    rightPadding = dip(10)
                    visibility = rightTextVisibility

                    rightView = textView {
                        text = rightText
                        textSize = dip(rightSize).toFloat()
                        textColorResource = rightTextColorRescource
                    }.lparams(width = wrapContent, height = wrapContent){
                        gravity = Gravity.CENTER
                    }
                }.lparams(width = wrapContent, height = matchParent){
                    gravity = Gravity.RIGHT  and Gravity.CENTER_VERTICAL
                }
            }
        }

        return titleRelativeLayout
    }
}